/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.ui_builders;

import br.ufes.cwtools.joinus.profile_manager.profile.Contact;
import br.ufes.cwtools.joinus.profile_manager.profile.Profile;
import br.ufes.cwtools.joinus.R;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

/*
 * this class does all the programatic UI work
 * it is also responsable for exporting a object containing the data
 * acquired from the UI  
 */
public class ContactUI extends ProfileUI {
	View myView;
	private boolean changed = false;

	public ContactUI() {
		this.layoutID = R.layout.contact;
	}

	public void SetUpSpinners(View v) {
		Spinner s = (Spinner) v.findViewById(R.id.im1_chooser);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				v.getContext(), R.array.ims, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		s = (Spinner) v.findViewById(R.id.im2_chooser);
		adapter = ArrayAdapter.createFromResource(
				v.getContext(), R.array.ims, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);
	}

	public void setUpWidgets(View v) {
		changed = true;
		myView = v;
		SetUpSpinners(v);
		SetupButtons();
	}

	public String toString() {
		return "Contact";
	}

	private void SetupButtons() {
		Button gtalk = (Button) myView.findViewById(R.id.google_talk_changer);
		gtalk.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					final Dialog humorDialog = new Dialog(v.getContext());
					humorDialog.setContentView(R.layout.gtalk_dialog);
					Button todo = (Button) humorDialog
							.findViewById(R.id.gtalk_quit);
					humorDialog.setTitle("Gtalk");
					todo.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							humorDialog.dismiss();
						}
					});
					humorDialog.show();
				}
			});

		Button sms = (Button) myView.findViewById(R.id.sms_chooser);
		sms.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog humorDialog = new Dialog(v.getContext());
				humorDialog.setContentView(R.layout.sms_dialog);
				Button todo = (Button) humorDialog.findViewById(R.id.sms_quit);
				humorDialog.setTitle("SMS");
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						humorDialog.dismiss();
					}
				});
				humorDialog.show();
			}
		});
	}

	// this method must be called after SetUpWidgets
	@Override
	public void importPro(Profile p) {
		Contact c = (Contact) p;
		((EditText) myView.findViewById(R.id.email1_text)).setText(c.priEmail);
		((EditText) myView.findViewById(R.id.email2_text)).setText(c.secEmail1);
		((EditText) myView.findViewById(R.id.email3_text)).setText(c.secEmail2);
		((EditText) myView.findViewById(R.id.email4_text)).setText(c.secEmail3);
		((EditText) myView.findViewById(R.id.im1_text)).setText(c.IM1user);
		((EditText) myView.findViewById(R.id.im2_text)).setText(c.IM2user);
		((Spinner) myView.findViewById(R.id.im1_chooser)).setSelection(c.IM1Server);
		((Spinner) myView.findViewById(R.id.im2_chooser)).setSelection(c.IM2Server);
		((EditText) myView.findViewById(R.id.home_phone_text)).setText(c.homePhone);
		((EditText) myView.findViewById(R.id.cell_phone_text)).setText(c.cellphone);
		((EditText) myView.findViewById(R.id.adress_line1_text)).setText(c.addr1);
		((EditText) myView.findViewById(R.id.adress_line2_text)).setText(c.addr2);
		((EditText) myView.findViewById(R.id.contact_city_text)).setText(c.city);
		((EditText) myView.findViewById(R.id.contact_state_text)).setText(c.state);
		((EditText) myView.findViewById(R.id.contact_zipcode_text)).setText(c.zipCode);
	}

	@Override
	public Profile export() {
		if (changed) {
			Contact contact = new Contact();
			contact.priEmail = ((EditText) myView.findViewById(R.id.email1_text)).getText().toString();
			contact.secEmail1 = ((EditText) myView.findViewById(R.id.email2_text)).getText().toString();
			contact.secEmail2 = ((EditText) myView.findViewById(R.id.email3_text)).getText().toString();
			contact.secEmail3 = ((EditText) myView.findViewById(R.id.email4_text)).getText().toString();
			contact.IM1user = ((EditText) myView.findViewById(R.id.im1_text)).getText().toString();
			contact.IM2user = ((EditText) myView.findViewById(R.id.im2_text)).getText().toString();
			contact.IM1Server = contact.IM1Server = ((Spinner) myView.findViewById(R.id.im1_chooser)).getSelectedItemPosition();
			contact.IM2Server = contact.IM2Server = ((Spinner) myView.findViewById(R.id.im2_chooser)).getSelectedItemPosition();
			contact.homePhone = ((EditText) myView.findViewById(R.id.home_phone_text)).getText().toString();
			contact.cellphone = ((EditText) myView.findViewById(R.id.cell_phone_text)).getText().toString();
			contact.addr1 = ((EditText) myView.findViewById(R.id.adress_line1_text)).getText().toString();
			contact.addr2 = ((EditText) myView.findViewById(R.id.adress_line2_text)).getText().toString();
			contact.city = ((EditText) myView.findViewById(R.id.contact_city_text)).getText().toString();
			contact.state = ((EditText) myView.findViewById(R.id.contact_state_text)).getText().toString();
			contact.zipCode = ((EditText) myView.findViewById(R.id.contact_zipcode_text)).getText().toString();
			return contact;
		}
		return null;
	}
}
